package cn.sh.ideal.web.system.management.user.management.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;



/**
* @author: zhangyy
* 2018年5月17日
* monitorCenterWeb--角色管理页面controller
*/

@Controller
@RequestMapping(value = "/data")
@PropertySource(value = {"classpath:jdbc.properties"})
public class RoleAndAuthorityManagementDataController {
	private RestTemplate rs = new RestTemplate();
	
	@Bean
	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}

	@Value("${server_address}")
	private String serverAddress;
	
	/**
	 * 查询所有角色
	 * 
	 * @param paramter
	 * @return result
	 * 前端ajax： contentType 默认不设定  request.getParameter可以获取参数
	 * get  方式向monitorCenter项目发出请求 参数以拼接方式传递 	@GetMapping("/getRoles") 接收
	 */
	@RequestMapping(value = "/getRoles", method = RequestMethod.POST)
	@ResponseBody
	public String getRoles(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String roleName=request.getParameter("roleName");
		int page=Integer.valueOf(request.getParameter("page"));
		int size=Integer.valueOf(request.getParameter("size"));
		StringBuffer url = new StringBuffer(serverAddress);
		url.append("/getRoles?roleName=").append(roleName).
		append("&page=").append(page).append("&pageSize=").append(size);

		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url.toString(),
				HttpMethod.GET, entity, String.class);

		String result = exchange.getBody();

		return result;
	}

	/**
	 * 创建角色
	 * 
	 * @param paramter
	 * @return result
	 * 前端ajax： contentType 默认不设定  可以用参数名获取参数获取参数
	 * get  方式向monitorCenter项目发出请求  MultiValueMap 传参 ；            @RequestParam接收参数
	 */
	@RequestMapping(value = "/createRole", method = RequestMethod.POST)
	@ResponseBody
	public String createRole(Date createTime,Date updateTime,String state,String code,String name,String description,String mIds) throws IOException{

		StringBuffer url = new StringBuffer(serverAddress);
				url.append("/createRole?createTime=").append(createTime).append("&updateTime=").append(updateTime).append("&state=").
				append(state).append("&code=" ).append(code).append("&name=").append(name).append("&description=").append(description).append("&mIds=").append(mIds);
//		
//		String url=serverAddress+"/createRole";
//		
//		MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
//        map.add("name", name);
//        map.add("description", description);
//        map.add("code", code);
//        map.add("state", state);
//        map.add("createTime", createTime);
//        map.add("updateTime", updateTime);
		// 端口调用
//		String result = rs.postForObject(url, map, String.class);
				
		String result = rs.getForObject(url.toString(), String.class);
		return result;
	}

	/**
	 * 删除角色
	 * 
	 * @param paramter
	 * @return result
	 */
	@RequestMapping(value = "/deleteRoles", method = RequestMethod.POST)
	@ResponseBody
	public String deleteRoles(String ids) throws IOException{
		String result ="";
		List<String> emp = new ArrayList<>();
		String[] sids = ids.split(",");
		for (int i = 0; i < sids.length; i++) {
			if (sids[i] != null || sids[i] != "") {
				emp.add(sids[i]);
			}
		}
		String url;
		if (emp.size()==1) {
			// 微服务接口调用
			 url = serverAddress+"/deleteRoleByIds?del_ids=";
			 ids=emp.get(0);

		} else {
			// 微服务接口调用
			 url = serverAddress+"/deleteRoleBatch?del_ids=";
		
		}
		result = rs.getForObject(url+ids, String.class);
		return result;
	}

	/**
	 * 更改角色权限
	 * 
	 * @param paramter
	 * @return result
	 */
	@RequestMapping(value = "/changeRolePermissions", method = RequestMethod.POST)
	@ResponseBody
	public String changeRolePermissions(
			String id, Date createTime,Date updateTime,
			String state,String code,String name,
			String description,String mIds)throws IOException {

		StringBuffer url = new StringBuffer(serverAddress);
		url.append("/changeRolePermissions?createTime=").append(createTime).append("&updateTime=").append(updateTime).append("&state=").
		append(state).append("&code=" ).append(code).append("&name=").append(name).append("&description=").append(description).append("&id=").append(id).append("&mIds=").append(mIds);
		// 端口调用
		String result = rs.getForObject(url.toString(), String.class);
		return result;
	}
	
	/**
	 * 查询menu列表整体
	 * @param paramter
	 * @return result
	 */
	@RequestMapping(value = "/getMenus", method = RequestMethod.POST)
	@ResponseBody
	public String getMenus()throws IOException {
		
		StringBuffer url = new StringBuffer(serverAddress);
		url.append("/getMenus");

		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);
		HttpEntity<String> entity = new HttpEntity<>(new String(), headers);

		ResponseEntity<String> exchange = rs.exchange(url.toString(),
				HttpMethod.GET, entity, String.class);

		String result = exchange.getBody();

		return result;
	}
}
